package com.tbit.uqbike.webmanager.controller;

import com.github.pagehelper.PageInfo;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.object.pojo.Account;
import com.tbit.uqbike.object.pojo.OperateLog;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.AccountService;
import com.tbit.uqbike.service.business.OperateLogService;
import com.tbit.uqbike.webmanager.util.ServletUtils;
import com.tbit.uqbike.webmanager.util.StringUtils;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 用户操作日志Controller
 *
 * @author jing
 * @date 2021-04-07
 */
@RestController
@RequestMapping("/operateLog")
public class OperateLogController extends BaseController {
    @Autowired
    private OperateLogService operateLogService;
    @Autowired
    private AccountService accountService;

    /**
     * 查询用户操作日志列表
     */
    @GetMapping("/list")
    public JsonResult list(OperateLog operateLog) {
        if (StringUtils.isNull(operateLog.getList())) {
            LoginInfo login = getLogin(ServletUtils.getRequest());
            operateLog.setList(login.type != 0 ? accountService.getAllAccountId(login.id).stream().map(Account::getAccountId).collect(Collectors.toList()) : null);
        }
        startPage();
        List<OperateLog> list = operateLogService.selectOperateLogList(operateLog);
        return JsonResult.ok(list, (int) new PageInfo<>(list).getTotal());
    }

    /**
     * 导出用户操作日志列表
     */
    @GetMapping("/export")
    public JsonResult export(OperateLog operateLog) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotNull(operateLog.getBeginTime())) {
            map.put("beginTime", operateLog.getBeginTime());
        }
        if (StringUtils.isNotNull(operateLog.getEndTime())) {
            map.put("endTime", operateLog.getEndTime());
        }
        operateLog.setParams(map);
        if (StringUtils.isNull(operateLog.getList())) {
            LoginInfo login = getLogin(ServletUtils.getRequest());
            operateLog.setList(login.type != 0 ? accountService.getAllAccountId(login.id).stream().map(Account::getAccountId).collect(Collectors.toList()) : null);
        }
        List<OperateLog> list = operateLogService.selectOperateLogList(operateLog);
        ExcelUtil<OperateLog> util = new ExcelUtil<>(OperateLog.class);
        return util.exportExcel(list, "操作日志");
    }

    /**
     * 获取选择框列表
     */
    @GetMapping("/optionselect")
    public JsonResult optionselect() {
        List<Map<String, Object>> result = new ArrayList<>();
        Arrays.stream(ModuleEnum.values()).
                filter(moduleEnum -> StringUtils.isNotEmpty(moduleEnum.getName()))
                .forEach(moduleEnum -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("name", moduleEnum.getName());
                    map.put("sn", moduleEnum.getSn());
                    result.add(map);
                });
        return JsonResult.ok(result);
    }


}